package com.smarth.zhizulian.utils.listhelper;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.smarth.zhizulian.utils.DialogUtils;
import com.smarth.zhizulian.utils.KumaToast;
import com.smarth.zhizulian.utils.listhelper.inter.IDataAdapter;
import com.smarth.zhizulian.utils.listhelper.inter.IDataModel;
import com.smarth.zhizulian.utils.listhelper.inter.ResponseSender;
import com.smarth.zhizulian.view.LoadingLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;


/**
 * Created by 18865 on 2018/1/11.
 */

public class RefreshHelper<T> implements ResponseSender<T> {
    private LoadingLayout mLoadingLayout;
    private RefreshLayout mRefreshLayout;
    private IDataModel<T> mDataModel;
    private IDataAdapter<T> mDataAdapter;
    private Context mContext;
    private int mControl = 0;
    public static final int CONTROL_LOADING = 0;
    public static final int CONTROL_REFRESH = 1;
    public static final int CONTROL_LOADMORE = 2;

    public RefreshHelper(Context context, LoadingLayout loadingLayout, RefreshLayout refreshLayout) {
        this.mLoadingLayout = loadingLayout;
        this.mRefreshLayout = refreshLayout;
        this.mContext = context;
        init();
    }

    private void init() {
        if (mRefreshLayout != null) {
            mRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
                @Override
                public void onRefresh(RefreshLayout refreshlayout) {
                    mControl = CONTROL_REFRESH;
                    mDataModel.refresh(RefreshHelper.this);
                }

                @Override
                public void onLoadmore(RefreshLayout refreshlayout) {
                    mControl = CONTROL_LOADMORE;
                    if (!mDataModel.hasMore()) {
                        KumaToast.show(mContext, "暂无数据");
                        finishContent();
                        return;
                    }
                    mDataModel.loadMore(RefreshHelper.this);
                }
            });
        }
        mLoadingLayout.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mControl = CONTROL_LOADING;
                mLoadingLayout.showLoading();
                mDataModel.refresh(RefreshHelper.this);
            }
        });
    }

    public IDataModel<T> getDataModel() {
        return mDataModel;
    }

    public void setDataModel(IDataModel<T> mDataModel) {
        this.mDataModel = mDataModel;
    }

    public IDataAdapter getDataAdapter() {
        return mDataAdapter;
    }

    public void setDataAdapter(IDataAdapter<T> mDataAdapter) {
        this.mDataAdapter = mDataAdapter;
        if (mRefreshLayout != null) {
            ViewGroup vg = mRefreshLayout.getLayout();
            for (int i = 0; i < vg.getChildCount(); i++) {
                View view = vg.getChildAt(i);
                if (vg.getChildAt(i) instanceof ListView) {
                    ListView lv = (ListView) vg.getChildAt(i);
                    lv.setAdapter((ListAdapter) mDataAdapter);
                    break;
                } else if (vg.getChildAt(i) instanceof RecyclerView) {
                    RecyclerView rc = (RecyclerView) vg.getChildAt(i);
                    ((BaseQuickAdapter) mDataAdapter).bindToRecyclerView(rc);
                    break;
                }
            }
        }
    }

    public void setDataAdapter(IDataAdapter<T> mDataAdapter, int listViewId) {
        this.mDataAdapter = mDataAdapter;
        if (mRefreshLayout != null) {
            View list = mRefreshLayout.getLayout().findViewById(listViewId);
            if (list instanceof ListView) {
                ListView lv = (ListView) list;
                lv.setAdapter((ListAdapter) mDataAdapter);
            } else if (list instanceof RecyclerView) {
                RecyclerView rc = (RecyclerView) list;
                ((BaseQuickAdapter) mDataAdapter).bindToRecyclerView(rc);
            }
        }
    }

    @Override
    public void sendError(Exception e) {
        finishError();
    }

    @Override
    public void sendData(T t) {
        mDataAdapter.onGetData(t, mRefreshLayout == null || mControl == CONTROL_LOADING ? true : mRefreshLayout.isRefreshing());
        finishContent(0);
    }

    @Override
    public void finishContent() {
        if (mRefreshLayout != null) {
            if (mRefreshLayout.isRefreshing()) {
                mRefreshLayout.finishRefresh();
            } else if (mRefreshLayout.isLoading()) {
                mRefreshLayout.finishLoadmore();
            }
        }
        if (mDataAdapter.isDataEmpty()) {
            mLoadingLayout.showEmpty();
        } else {
            mLoadingLayout.showContent();
        }
        DialogUtils.closeProgressDialog();
    }

    public void finishContent(int delay) {
        if (mRefreshLayout != null) {
            if (mRefreshLayout.isRefreshing()) {
                mRefreshLayout.finishRefresh(delay);
            } else if (mRefreshLayout.isLoading()) {
                mRefreshLayout.finishLoadmore();
            }
        }
        if (mDataAdapter.isDataEmpty()) {
            mLoadingLayout.showEmpty();
        } else {
            mLoadingLayout.showContent();
        }
        DialogUtils.closeProgressDialog();
    }

    public void finishContent(int delayRefresh, int delayLoadmore) {
        if (mRefreshLayout != null) {
            if (mRefreshLayout.isRefreshing()) {
                mRefreshLayout.finishRefresh(delayRefresh);
            } else if (mRefreshLayout.isLoading()) {
                mRefreshLayout.finishLoadmore(delayLoadmore);
            }
        }
        if (mDataAdapter.isDataEmpty()) {
            mLoadingLayout.showEmpty();
        } else {
            mLoadingLayout.showContent();
        }
        DialogUtils.closeProgressDialog();
    }

    @Override
    public void finishError() {
        if (mRefreshLayout != null) {
            if (mRefreshLayout.isRefreshing()) {
                mRefreshLayout.finishRefresh();
            } else if (mRefreshLayout.isLoading()) {
                mRefreshLayout.finishLoadmore();
            }
        }
        mLoadingLayout.showError();
        DialogUtils.closeProgressDialog();
    }

//    public void refresh() {
//        if (mRefreshLayout == null || mDataAdapter.isDataEmpty()) {
//            mControl = CONTROL_LOADING;
//            mLoadingLayout.showLoading();
//            mDataModel.refresh(RefreshHelper.this);
//        } else {
//            mRefreshLayout.autoRefresh();
//        }
//    }

    public void refresh() {
        if (mRefreshLayout == null) {
            mControl = CONTROL_LOADING;
            if (mDataAdapter.isDataEmpty()) {
                mLoadingLayout.showLoading();
            }
            mDataModel.refresh(RefreshHelper.this);
        } else {
            if (mDataAdapter.isDataEmpty()) {
                mControl = CONTROL_LOADING;
                mLoadingLayout.showLoading();
                mDataModel.refresh(RefreshHelper.this);
            } else {
                mControl = CONTROL_REFRESH;
                mRefreshLayout.autoRefresh();
            }
        }
    }

    public void loadMore() {
        if (mRefreshLayout == null) {
            mControl = CONTROL_LOADING;
            DialogUtils.showNetWorkAccess(mContext);
            mDataModel.refresh(RefreshHelper.this);
        } else {
            if (!mDataModel.hasMore()) {
                KumaToast.show(mContext, "暂无数据");
                finishContent();
                return;
            }
            mRefreshLayout.autoLoadmore();
        }
    }

    public void setOnRetryEmptyListener(View.OnClickListener onRetryEmptyListener) {
        mLoadingLayout.setOnRetryEmptyClickListener(onRetryEmptyListener);
    }

    public int getControl() {
        return mControl;
    }

    public void destroy() {
        mLoadingLayout = null;
    }
}
